package com.jsoft.controller;

import com.jsoft.annotation.Log;
import com.jsoft.constant.Constants;
import com.jsoft.entity.Degree;
import com.jsoft.entity.User;
import com.jsoft.exception.BusinessException;
import com.jsoft.service.DegreeService;
import com.jsoft.service.UserService;
import com.jsoft.util.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * (Degree)表控制层
 *
 * @author syr
 * @since 2025-03-14 23:30:25
 */
@RestController
@RequestMapping("degree")
public class DegreeController {
    /**
     * 服务对象
     */
    @Resource
    private DegreeService degreeService;

    @Resource
    private UserService userService;

    /**
     * 通过用户名查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping
    public Result<Degree> queryByUsername() {
        //根据请求头中的用户名去用户表查询用户信息
        List<User> userList = getUserList();
        //如果查不到，说明用户名错误
        if( userList == null || userList.isEmpty() ){
            throw new BusinessException("用户名错误", Code.GET_ERR);
        }
        //如果查到了，根据用户主键去学历表查询
        User user = userList.get(0);
        Degree de = degreeService.queryByUserId(user.getId());
        if( de == null ){
            //该用户未填写学历信息，返回空数据
            return Result.success();
        }else{
            //用户已填写基本信息，返回查询到的数据
            return Result.success(de);
        }
    }

    /**
     * 新增数据
     *
     * @param degree 实体
     * @return 新增结果
     */
    @Log(title = "post",businessType = "新增学历信息")
    @PostMapping
    public Result<Degree> add(@RequestBody Degree degree) {
        //根据请求头中的用户名去用户表查询用户信息
        List<User> userList = getUserList();
        //如果查不到，说明用户名错误
        if( userList == null || userList.isEmpty() ){
            throw new BusinessException("用户名错误", Code.GET_ERR);
        }
        //如果查到了，根据用户主键去学历表查询
        User user = userList.get(0);
        Degree de = degreeService.queryByUserId(user.getId());
        if( de == null ){
            //该用户未填写学历信息，可以新增
            degree.setUserId(user.getId());
            degree.setUpdateTime(new Date());
            degree.setStatus(0);
            degreeService.insert(degree);
            return Result.success();
        }else{
            //用户已填写学历信息，不能新增
            throw new BusinessException("请勿重复提交", Code.SAVE_ERR);
        }
    }

    /**
     * 编辑数据
     *
     * @param degree 实体
     * @return 编辑结果
     */
    @Log(title = "put",businessType = "修改学历信息")
    @PutMapping
    public Result<Degree> edit(@RequestBody Degree degree) {
        //根据请求头中的用户名去用户表查询用户信息
        List<User> userList = getUserList();
        //如果查不到，说明用户名错误
        if( userList == null || userList.isEmpty() ){
            throw new BusinessException("用户名错误", Code.GET_ERR);
        }
        //如果查到了，根据用户主键去学历表修改数据
        User user = userList.get(0);
        degree.setUserId(user.getId());
        degree.setUpdateTime(new Date());
        degreeService.update(degree);
        return Result.success();
    }

    /**
     * 根据请求头中的用户名去用户表查询用户信息
     * @return 用户集合
     */
    public List<User> getUserList(){
        //从请求头中获取当前用户名
        HttpServletRequest request =
                ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String username = request.getHeader(Constants.HEADER_USERNAME);
        //根据用户名去数据库查询userList
        List<User> userList = userService.queryByUsername(username);
        return userList;
    }

}